package message

import (
	"errors"
	"regexp"
	"strconv"
	"time"
)

func ParseMessage(m string) (*Message,error) {
	msg := &Message{}
	p := `([\d\.]*)\s+[^ ]+\s+[^ ]+\s+\[([^\]]+)\]\s+\"([^\ ]+)\s+([^ ]+)\s+([^\"]+)\"\s+(\d+)\s+(\d+)\s+\"[^\"]+\"\s+\"[^\(]*\s+\(([^\)]+)\)\s+[^ ]+\s+\([^\)]+\)\s+([^\"]+)`
	r := regexp.MustCompile(p)
	ret := r.FindStringSubmatch(m)

	if len(ret) != 10 {
		return nil,errors.New("regexp complie err")
	}


	loc, _ := time.LoadLocation("Local")
	t,_ := time.ParseInLocation("02/Jan/2006:15:04:05 +0800",ret[2],loc)
	msg.TimeLocal = t


	msg.Address = ret[1]
	msg.Method = ret[3]
	msg.Path = ret[4]
	msg.Scheme = ret[5]
	msg.Status = ret[6]

	msg.Size, _ = strconv.Atoi(ret[7])

	msg.Os = ret[8]
	msg.Browser = ret[9]

	return msg,nil

}
